package pri.damai.xiaowu.anydoor.business.groupchat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import pri.damai.xiaowu.anydoor.business.BaseModel;
import pri.damai.xiaowu.anydoor.netty.ChannelStore;

import java.util.List;

/**
 *
 * @Desc
 * @Author yxf
 * @Date 2022/7/27 21:20
 * 但行好事，莫问前程。
 */
@Data
public class GroupSendReq implements BaseModel {

    private String groupId;
    private String content;

    @Override
    public Byte code() {
        return 17;
    }

    @Override
    public void process(ChannelHandlerContext ctx) {
        List<Integer> userIds = ChannelStore.GROUP_USER_MAP.get(this.getGroupId());

        userIds.forEach(userId -> {
            Channel channel = ChannelStore.getUserChannel(userId);
            GroupSendRev groupSendRev = new GroupSendRev();
            groupSendRev.setGroupId(groupSendRev.getGroupId());
            groupSendRev.setContent(this.getContent());
            channel.writeAndFlush(groupSendRev);
        });

        GroupSendRsp rsp = new GroupSendRsp();
        ctx.writeAndFlush(rsp);
    }
}
